package com.mianyang.zhangsan.common.aspect;

import com.mianyang.zhangsan.auth.config.RsaKeyProperties;
import com.mianyang.zhangsan.common.annotation.SaveLog;
import com.mianyang.zhangsan.common.constant.SecConstant;
import com.mianyang.zhangsan.common.util.JwtUtils;
import com.mianyang.zhangsan.common.util.OperationManHelper;
import com.mianyang.zhangsan.common.util.Payload;
import com.mianyang.zhangsan.model.entity.AccountDetail;
import com.mianyang.zhangsan.model.entity.SecAccount;
import com.mianyang.zhangsan.model.entity.SecSystemLog;
import com.mianyang.zhangsan.service.SecSystemLogService;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Date;
import java.util.Optional;

/**
 * 日志记录切面
 * @author jiangjr
 * @date 2022-11-11
 */
@Aspect
@Component
public class SystemLogAspect {
    @Autowired
    private SecSystemLogService logService;

    @Pointcut("@annotation(com.mianyang.zhangsan.common.annotation.SaveLog)")
    public void logPointCut(){}

    @AfterReturning(value = "logPointCut()", returning = "result")
    public void saveNormalLog(JoinPoint joinPoint, Object result) {
        // 获取请求
        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
        // 获取httpServletRequest
        if (requestAttributes != null) {
            HttpServletRequest request =
                    (HttpServletRequest)requestAttributes.resolveReference(RequestAttributes.REFERENCE_REQUEST);
            MethodSignature signature = (MethodSignature) joinPoint.getSignature();
            Method method = signature.getMethod();
            if (request != null) {
                SaveLog saveLog = method.getAnnotation(SaveLog.class);
                // 获取操作员信息
                SecAccount account = null;
                String operationMan = OperationManHelper.getAccountName();
                // 如果是登录接口
                if (SecConstant.SEC_LOGIN_ADDR.equals(method.getName())) {
                    Object[] args = joinPoint.getArgs();
                    if (args.length > 0) {
                        account = (SecAccount) args[0];
                        operationMan = account.getAccountName();
                    }
                }
                SecSystemLog systemLog = SecSystemLog.builder()
                        .title(saveLog.title())
                        .content(saveLog.content())
                        .accountId(0L)
                        .accountName(operationMan)
                        .requestUrl(request.getRequestURI())
                        .occurTime(new Date()).build();
                logService.save(systemLog);
            }
        }
    }

//    @AfterThrowing(value = "logPointCut()", throwing="e")
//    public void saveExceptionLog(JoinPoint joinPoint, Throwable e) {
//
//    }
}
